<?php
namespace App\Http\Controllers\Admin;
use App\Models\MemberQnRisk;
use App\Models\Order;
use App\Models\OrderRiskAlipay;
use App\Models\OtherOrderAllocate;
use App\Models\RiskCredit;
use App\Models\ShopUser;
use App\Models\SmsRecord;
use App\Models\SmsTemplate;
use App\Models\UnicomOrder;
use App\Services\CommonService;
use App\Services\ExportService;
use App\Services\UnicomService;
use Illuminate\Http\Request;

class DataController extends BaseController
{
    //获取短信发送场景 http://shop.cn/Admin/Data/getSmsTemplate
    public function getSmsTemplate()
    {
        $sms_template = SmsTemplate::select('id', 'sms_title')->get();
        $sms_template = $sms_template ? $sms_template->toArray() : [];
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $sms_template);
    }

    //短信使用列表POST
    public function msmUseList(Request $request)
    {
        $input = $request->input();
        $where = [];
        if (!empty($input['mobile'])) {
            $where[] = ['sr.mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        if (!empty($input['template_id'])) {
            $where[] = ['sr.template_id', $input['template_id']];
        }
        if (!empty($input['send_status'])) {
            $where[] = ['sr.send_status', $input['send_status']];
        }
        if (!empty($input['shopname'])) {
            $where[] = ['su.shopname', 'like', '%' . trim($input['shopname']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['sr.create_time', '>=', $time['begin_time']];
            $where[] = ['sr.create_time', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = SmsRecord::GetSmsRecordList($pageSize, $where);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            $info[$k]['send_status_name'] = SmsRecord::getSendStatus($v['send_status']); //发送状态
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }


    /**
     * 百融风控使用记录
     */
    public function riskUseList(Request $request)
    {
        $input = $request->input();
        $where = [];
        if (!empty($input['order_id'])) {
            $where[] = ['rc.trade_no', 'like', '%' . trim($input['order_id']) . '%'];
        }
        if (!empty($input['type'])) {
            $where[] = ['rc.type', $input['type']];
        }
        if (!empty($input['user_name'])) {
            $where[] = ['rc.user_name', $input['user_name']];
        }
        if (!empty($input['user_mobile'])) {
            $where[] = ['rc.user_mobile', 'like', '%' . trim($input['user_mobile']) . '%'];
        }
        if (!empty($input['shopname'])) {
            $where[] = ['su.shopname', 'like', '%' . trim($input['shopname']) . '%'];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['rc.create_time', '>=', $time['begin_time']];
            $where[] = ['rc.create_time', '<', $time['end_time']];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        $data = RiskCredit::getRiskCreditList($page,$pageSize, $where);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //联通卡使用列表POST
    public function unicomLists(Request $request)
    {
        $input = $request->input();
        $where = [];
        $where[] = ['status', $input['status']];
        if (!empty($input['mobile'])) {
            $where[] = ['mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        if (!empty($input['certName'])) {
            $where[] = ['certName', 'like', '%' . trim($input['certName']) . '%'];
        }
        if (!empty($input['certId'])) {
            $where[] = ['certId', 'like', '%' . trim($input['certId']) . '%'];
        }
        if (!empty($input['order_id'])) {
            $where[] = ['order_id', trim($input['order_id'])];
        }
        if (!empty($input['create_time'])) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', strtotime($time['begin_time'])];
            $where[] = ['create_time', '<', strtotime($time['end_time'])];
        }
        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        // $pageSize = 30;
        // dd($where);
        $data = UnicomOrder::getUnicomList($pageSize, $where);
        // dd($data);
        $data = formatPage($data);
        $info = $data['data'];
        foreach ($info as $k => $v) {
            if($v['result'] && $v['status'] == UnicomOrder::UNICOM_STATUS_ONE){ //同步成功
                $info[$k]['stutas_remark'] = '同步成功';
            }else if($v['result'] && $v['status'] == UnicomOrder::UNICOM_STATUS_TWO){ //同步失败
                $info[$k]['stutas_remark'] = '同步失败';
            }else if(!$v['result'] && $v['status'] == UnicomOrder::UNICOM_STATUS_TWO){ //未同步
                $info[$k]['stutas_remark'] = '等待同步';
            }else if(!$v['result'] && $v['status'] == UnicomOrder::UNICOM_STATUS_THREE){ //未选号
                $info[$k]['stutas_remark'] = '等待选号';
            }else{
                $info[$k]['stutas_remark'] = '其他状态';
            }

            $info[$k]['goods'] = UnicomService::getUnicomGoodsName($v['goodsId']);

            unset($v['result']);
            unset($v['goodsId']);
            unset($v['status']);
        }
        $data['data'] = $info;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }
    //联通卡邮寄轨迹查询
    public function unicomExpressTrace(Request $request)
    {
        $input = $request->input();

        $data = UnicomService::unicomExpressTrace($input);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);

    }

    /**
     * 支付宝风险评估使用记录
     */
    public function riskAlipayList(Request $request)
    {
        $input = $request->input();
        $where = [];

        if (!empty($input['cert_name'])) {
            $where[] = ['cert_name', $input['cert_name']];
        }
        if (!empty($input['mobile'])) {
            $where[] = ['mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        if (!empty($input['cert_no'])) {
            $where[] = ['cert_no', 'like', '%' . trim($input['cert_no']) . '%'];
        }

        if (!empty($input['status'])) {
            $where[] = ['status', $input['status']];
        }

        if (!empty($input['create_time'])&&$input['type']==1) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', $time['begin_time']];
            $where[] = ['create_time', '<', $time['end_time']];
        }
        if (!empty($input['create_time'])&&$input['type']==2) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', strtotime($time['begin_time'])];
            $where[] = ['create_time', '<', strtotime($time['end_time'])];
        }
        if (!empty($input['shopname'])) {
            $member_id=OtherOrderAllocate::where('name',$request['shopname'])->value('member_id');
            $membera_id=ShopUser::where('shopname',$request['shopname'])->value('member_id');
            if($member_id){
                $where[] = ['member_id', $member_id];
            }else if($membera_id){
                $where[] = ['member_id', $membera_id];
            }else{
                $where[] = ['member_id', -1];
            }
        }

        if (!empty($input['result'])&&$input['type']==2) {
            $where[] = ['risk_result', $input['result']];
        }


        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        if (!empty($input['type'])&&$input['type']==1) {
            $data = OrderRiskAlipay::getRiskAlipayList($page,$pageSize, $where);
        }
        if (!empty($input['type'])&&$input['type']==2) {
            $data =  MemberQnRisk::getRiskQnList($page,$pageSize, $request);
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $data);
    }

    //导出数据

    public function riskAlipayExport(Request $request)
    {
        $input = $request->input();
        $where = [];

        if (!empty($input['cert_name'])) {
            $where[] = ['cert_name', $input['cert_name']];
        }
        if (!empty($input['mobile'])) {
            $where[] = ['mobile', 'like', '%' . trim($input['mobile']) . '%'];
        }
        if (!empty($input['cert_no'])) {
            $where[] = ['cert_no', 'like', '%' . trim($input['cert_no']) . '%'];
        }

        if (!empty($input['status'])) {
            $where[] = ['status', $input['status']];
        }

        if (!empty($input['create_time'])&&$input['type']==1) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', $time['begin_time']];
            $where[] = ['create_time', '<', $time['end_time']];
        }
        if (!empty($input['create_time'])&&$input['type']==2) {
            $time = searchTime($input['create_time']);
            $where[] = ['create_time', '>=', strtotime($time['begin_time'])];
            $where[] = ['create_time', '<', strtotime($time['end_time'])];
        }
        if (!empty($input['shopname'])) {
            $member_id=OtherOrderAllocate::where('name',$request['shopname'])->value('member_id');
            $membera_id=ShopUser::where('shopname',$request['shopname'])->value('member_id');
            if($member_id){
                $where[] = ['member_id', $member_id];
            }else if($membera_id){
                $where[] = ['member_id', $membera_id];
            }else{
                $where[] = ['member_id', -1];
            }
        }

        if (!empty($input['result'])&&$input['type']==2) {
            $where[] = ['risk_result', $input['result']];
        }


        $page = $input['page'] ? $input['page'] : 1;
        $pageSize = $input['pageSize'] ? $input['pageSize'] : CommonService::PAGESIZE;
        if (!empty($input['type'])&&$input['type']==1) {
            $data = OrderRiskAlipay::getRiskAlipayList($page,$pageSize, $where);
        }
        if (!empty($input['type'])&&$input['type']==2) {
            $data =  MemberQnRisk::getRiskQnListAll($page,$pageSize, $request);
        }


        ExportService::exportFile($input,$data,ExportService::RISK_DATA_EXPORT);


    }

}
